package org.alvin.i18n.newui;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;


public class I18nUtilsV2 {

    public static void main(String[] args) throws IOException {
        String[] dirs = {"D:\\tangzhichao\\workspace\\xiaolan\\electrombileweb\\xblue_v1\\src\\zv3newui\\quanda\\message"};
        List<String> excluds = Lists.newArrayList("App.vue");
        File outFile = new File("D:/i18n.js");
        File outFileEn = new File("D:/i18n_en.js");
        String version = "v2";
        String callStr = "$t";
        I18nV2ServiceV2 i18NV2ServiceV2 = new I18nV2ServiceV2();
        if (version.equals("v3")) {
            callStr = "t";
            i18NV2ServiceV2 = new I18nV3ServiceV2();
        }
        JSONObject result = new JSONObject();
        LinkedList<String> list = Lists.newLinkedList();
        for (String dir : dirs) {
            list.add(dir);
        }
        int count = 0;
        fileLabel:
        while (!list.isEmpty()) {
            String file = list.removeFirst();
            File f = new File(file);
            if (f.isDirectory()) {
                if (f.getAbsolutePath().contains("newview")) {
                    continue;
                }
                if (f.listFiles() != null) {
                    for (File s : f.listFiles()) {
                        list.add(s.getAbsolutePath());
                    }
                }
                continue;
            }
            if (!f.getName().toLowerCase().endsWith(".vue")) {
                continue;
            }
            // 过滤，临时先解决问题
            if (excluds.contains(f.getName())) {
                System.out.println("过滤文件-----" + f.getName());
                continue fileLabel;
            }

            System.out.println(count++);
            System.out.println("处理文件:" + f.getAbsolutePath());
            String fileName = f.getName();
            fileName = fileName.substring(0, fileName.lastIndexOf("."));
            JSONObject obj = i18NV2ServiceV2.tranfer(f, fileName, callStr);
            if(obj.isEmpty()){
                continue ;
            }
            result.put(fileName, obj);
        }
        String formatStr = JSON.toJSONString(result, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteDateUseDateFormat);
        Files.write(Paths.get(outFile.getAbsolutePath()), formatStr.getBytes());
        // 自动翻译成英文
//        translate(result, outFileEn);
    }

    /**
     * 自动翻译
     *
     * @param source
     * @param outFile
     * @throws IOException
     */
    private static void translate(JSONObject source, File outFile) throws IOException {
        for (String key : source.keySet()) {
            JSONObject dataJson = source.getJSONObject(key);
            for (String dataKey : dataJson.keySet()) {
                String value = dataJson.getString(dataKey);
                String english = I18nDictionary.get(dataKey, value);
                if (!Strings.isNullOrEmpty(english)) {
                    dataJson.put(dataKey, english);
                }
            }
        }
        String formatStr = JSON.toJSONString(source, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteDateUseDateFormat);
        Files.write(Paths.get(outFile.getAbsolutePath()), formatStr.getBytes());
    }


}
